package action;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.connector.Request;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import service.BookManager;
import vo.User;

public class CommonAction extends ActionSupport{
	
	private User u;
	private String path;
	private File head;
	private String headFileName;
	private String headContentType;
	
	private BookManager bm;
	
	
	
	public void setBm(BookManager bm) {
		this.bm = bm;
	}

	@Override
	public void validate() {
		// TODO Auto-generated method stub
		super.validate();
	}
	
	public String getFileName(){
		return "john.jpg";
	}
	public InputStream getFile() {
		
		InputStream in=null;
		try {
			in = new FileInputStream(path+"a.jpg");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return in;
	}
	public String download(){
		return SUCCESS;
	}
	
	
	public String getHeadFileName() {
		return headFileName;
	}


	public void setHeadFileName(String headFileName) {
		this.headFileName = headFileName;
	}


	public String getHeadContentType() {
		return headContentType;
	}


	public void setHeadContentType(String headContentType) {
		this.headContentType = headContentType;
	}


	public File getHead() {
		return head;
	}


	public void setHead(File head) {
		this.head = head;
	}


	public User getU() {
		return u;
	}


	public void setU(User u) {
		this.u = u;
	}


	public CommonAction(){
		//path=ServletActionContext.getServletContext().getRealPath("/download/");
		//System.out.println(path);
		
	}
	
	public String upload() throws Exception{
		String headPath=head.getAbsolutePath();
		System.out.println(headContentType);
		InputStream oin=new FileInputStream(headPath);
		byte[] buf=new byte[oin.available()];
		oin.read(buf);
		FileOutputStream out=new FileOutputStream(path+headFileName);
		out.write(buf);
		oin.close();
		out.close();
		return "success";
	}
	
	public String regist(){
		return "success";
	}
	public String login(){
		System.out.println("login.......");
		
		bm.addBook();
		/*
		System.out.println("login....."+u.getUname()+"....."+u.getAge());
		ActionContext.getContext().put("u", u);
		ActionContext.getContext().getSession().put("u1", u);
		ActionContext.getContext().getApplication().put("u2", u);
		*/
		//ServletActionContext.getServletContext()
		//ServletActionContext.getRequest().getSession()
		
		return "success";
	}
	
	public String find(){
		System.out.println("find.........");
		return "success";
	}
	
	public String checkCode(){
		
		return "success";
	}
	
	public InputStream getCode() {
		
		InputStream in=null;
		try {
			in = new FileInputStream(path+"a.jpg");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return in;
	}
	
	public String jsonQuery(){
		return "success";
	}
	public InputStream getJson(){
		String json="{uname:john,age:100}";
		return new ByteArrayInputStream(json.getBytes());
				
	}
	

}
